﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Text;
using System.Collections;

namespace Data.ServiceAgents.ActiveDirectoryAccess
{
    public class ActiveDirectory
    {
        protected string str_ad_user;
        protected string str_ad_password;
        protected string str_domain;
        protected string str_ldap_connection;

        public ActiveDirectory(string str_domain)
        {
            this.str_domain = str_domain;
            this.str_ldap_connection = "LDAP://" + this.str_domain;
            this.str_ad_user = string.Empty;
            this.str_ad_password = string.Empty;
        }

        public ActiveDirectory(string str_domain, string str_ad_user, string str_ad_password)
        {
            this.str_domain = str_domain;
            this.str_ldap_connection = "LDAP://" + this.str_domain;
            this.str_ad_user = str_ad_user;
            this.str_ad_password = str_ad_password;
        }

        public string getUser(string str_full_user_name)
        {
            string[] str_user_path = str_full_user_name.Split(new char[] { '\\' });
            return str_user_path[str_user_path.Length - 1];
        }

        public bool exists(string str_login)
        {
            DirectoryEntry oDirectoryEntry;

            if (this.str_ad_user != string.Empty && this.str_ad_password != string.Empty)
                oDirectoryEntry = new DirectoryEntry(this.str_ldap_connection, this.str_ad_user, this.str_ad_password);
            else
                oDirectoryEntry = new DirectoryEntry(this.str_ldap_connection);

            DirectorySearcher oDirectorySearcher = new DirectorySearcher(oDirectoryEntry);

            string str_user = this.getUser(str_login);
            oDirectorySearcher.Filter = String.Format("(SAMAccountName={0})", str_user);
            oDirectorySearcher.PropertiesToLoad.Add("cn");
            SearchResult oSearchResult = oDirectorySearcher.FindOne();

            if (oSearchResult == null)
                return false;

            return true;
        }

        public string getEmail(string str_full_user_name)
        {
            DirectoryEntry oDirectoryEntry;

            if (this.str_ad_user != string.Empty && this.str_ad_password != string.Empty)
                oDirectoryEntry = new DirectoryEntry(this.str_ldap_connection, this.str_ad_user, this.str_ad_password);
            else
                oDirectoryEntry = new DirectoryEntry(this.str_ldap_connection);

            DirectorySearcher oDirectorySearcher = new DirectorySearcher(oDirectoryEntry);

            string str_user = this.getUser(str_full_user_name);

            oDirectorySearcher.Filter = String.Format("(SAMAccountName={0})", str_user);

            this.loadSerchProperties(oDirectorySearcher);

            SearchResult oSearchResult = oDirectorySearcher.FindOne();

            string str_email = string.Empty;

            if (oSearchResult != null && oSearchResult.Properties.Contains("mail"))
                str_email = oSearchResult.Properties["mail"][0].ToString();

            return str_email;
        }

        private void loadSerchProperties(DirectorySearcher oDirectorySearcher)
        {
            oDirectorySearcher.PropertiesToLoad.Add("SAMAccountName");
            //oDirectorySearcher.PropertiesToLoad.Add("givenname");
            //oDirectorySearcher.PropertiesToLoad.Add("SN");
            oDirectorySearcher.PropertiesToLoad.Add("mail");
            //oDirectorySearcher.PropertiesToLoad.Add("homePhone");
            //oDirectorySearcher.PropertiesToLoad.Add("streetAddress");
            //oDirectorySearcher.PropertiesToLoad.Add("mobile");
            //oDirectorySearcher.PropertiesToLoad.Add("company");
            //oDirectorySearcher.PropertiesToLoad.Add("department");
            //oDirectorySearcher.PropertiesToLoad.Add("l");
            //oDirectorySearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName");
            //oDirectorySearcher.PropertiesToLoad.Add("title");
            //oDirectorySearcher.PropertiesToLoad.Add("telephoneNumber");
            //oDirectorySearcher.PropertiesToLoad.Add("CN");
            //oDirectorySearcher.PropertiesToLoad.Add("distinguishedName");
        }
    }
}